Skip to content

Add DatasetsV2Client (WIP) - #1

Open
Hellebore wants to merge 1 commit into
base-sha/ede65018a97676464339abed00c6b514fe2c35bffrom
head-sha/085d2ff693964f06276df24d5840b0a9f5593bd9/2025-05-02T11-40-15/55ea09
Open

Add DatasetsV2Client (WIP)#1
Hellebore wants to merge 1 commit into
base-sha/ede65018a97676464339abed00c6b514fe2c35bffrom
head-sha/085d2ff693964f06276df24d5840b0a9f5593bd9/2025-05-02T11-40-15/55ea09

Conversation

@Hellebore

@Hellebore Hellebore commented May 2, 2025

Copy link
Copy Markdown

Greptile Summary

This PR adds a comprehensive DatasetsV2Client implementation for the Autoblocks Python SDK, providing CRUD operations for datasets with schema versioning and validation support.

  • Empty autoblocks/_impl/datasets_v2/__init__.py needs to be populated with proper re-exports to expose the package's public API
  • Client implementation in client.py should enhance error handling and input validation, particularly around URL construction and special characters
  • Test coverage in test_client.py should be expanded to include error cases, timeout handling, and URL encoding validation
  • Consider adding more robust error handling and user guidance in datasets_v2_example.py

The implementation is well-structured with good type definitions and validation logic, but these improvements would make it more robust and production-ready.

Summary by Sourcery

Implement a comprehensive DatasetsV2Client for the Autoblocks Python SDK, providing robust CRUD operations for datasets with schema versioning, validation, and type-safe interactions.

New Features:

  • Add DatasetsV2Client with full CRUD operations for datasets
  • Implement conversation validation and schema property types
  • Create type-safe dataset and item management interfaces

Enhancements:

  • Robust error handling and input validation
  • URL encoding for dataset and item identifiers
  • Comprehensive type definitions for datasets and schema properties

Documentation:

  • Add example script demonstrating datasets v2 client usage
  • Provide comprehensive type hints and docstrings

Tests:

  • Add unit tests for client methods
  • Implement conversation validation tests
  • Test dataset creation, item management, and schema interactions

@Hellebore

Copy link
Copy Markdown
Author

This is a benchmark review for experiment bakeoff.
Run ID: bakeoff/benchmark_2025-05-02T11-38-04_v1-36-0-dirty.

This pull request was cloned from https://github.com/autoblocksai/python-sdk/pull/335. (Note: the URL is not a link to avoid triggering a notification on the original pull request.)

Experiment configuration
review_config:
  # User configuration for the review
  # - benchmark - use the user config from the benchmark reviews
  # - <value> - use the value directly
  user_review_config:
    enable_ai_review: true
    enable_rule_comments: false

    enable_complexity_comments: benchmark
    enable_security_comments: benchmark
    enable_tests_comments: benchmark
    enable_comment_suggestions: benchmark

    enable_pull_request_summary: benchmark
    enable_review_guide: benchmark

    enable_approvals: false
    base_branches: [base-sha.*]

  ai_review_config:
    # The model responses to use for the experiment
    # - benchmark - use the model responses from the benchmark reviews
    # - llm - call the language model to generate responses
    model_responses:
      comments_model: benchmark
      comment_validation_model: benchmark
      comment_suggestion_model: benchmark
      complexity_model: benchmark
      security_model: benchmark
      tests_model: benchmark
      pull_request_summary_model: benchmark
      review_guide_model: benchmark
      overall_comments_model: benchmark

# The pull request dataset to run the experiment on
pull_request_dataset:
# CodeRabbit
- https://github.com/neerajkumar161/node-coveralls-integration/pull/5
- https://github.com/gunner95/vertx-rest/pull/1
- https://github.com/Altinn/altinn-access-management-frontend/pull/1427
- https://github.com/theMr17/github-notifier/pull/14
- https://github.com/bearycool11/AI_memory_Loops/pull/142

# Greptile
- https://github.com/gumloop/guMCP/pull/119
- https://github.com/autoblocksai/python-sdk/pull/335
- https://github.com/grepdemos/ImageSharp/pull/6
- https://github.com/grepdemos/server/pull/61
- https://github.com/websentry-ai/pipelines/pull/25

# Graphite
- https://github.com/KittyCAD/modeling-app/pull/6648
- https://github.com/KittyCAD/modeling-app/pull/6628
- https://github.com/Varedis-Org/AI-Test-Repo/pull/2
- https://github.com/deeep-network/bedrock/pull/198
- https://github.com/Metta-AI/metta/pull/277

# Copilot
- https://github.com/hmcts/rpx-xui-webapp/pull/4438
- https://github.com/ganchdev/quez/pull/104
- https://github.com/xbcsmith/ymlfxr/pull/13
- https://github.com/tinapayy/B-1N1T/pull/36
- https://github.com/coder/devcontainer-features/pull/6

# Questions to ask to label the review comments
review_comment_labels: []
# - label: correct
#   question: Is this comment correct?

# Benchmark reviews generated by running
#   python -m scripts.experiment benchmark <experiment_name>
benchmark_reviews: []

@Hellebore

Copy link
Copy Markdown
Author

Reviewer's Guide

This pull request implements the DatasetsV2Client by adding new modules for the client logic, data types, schema definitions, and validation within the autoblocks._impl.datasets_v2 directory. It uses the requests library for API communication, dataclasses for data modeling, and exposes the functionality publicly through re-exports in autoblocks/datasets_v2. Unit tests using pytest and unittest.mock are included, along with an example script.

File-Level Changes

Change Details Files
Implemented DatasetsV2Client for Datasets V2 API interactions.
  • Added methods for dataset and item CRUD operations (list, create, destroy, get, update).
  • Handles API requests using the requests library.
  • Includes helper for dataclass serialization and URL encoding.
  • Added factory function create_datasets_v2_client.
  • Configured base URL, authentication, and headers.
  • Implemented basic error handling for HTTP responses.
  • Established public export for the client.
autoblocks/_impl/datasets_v2/client.py
autoblocks/datasets_v2/client.py
autoblocks/datasets_v2/__init__.py
Defined data structures and schema types using dataclasses.
  • Created dataclasses for API request/response bodies (e.g., DatasetV2, CreateDatasetV2Request, DatasetItemV2).
  • Defined specific types for conversations (Conversation, ConversationTurn, ConversationMessage).
  • Defined dataclasses for various schema property types (e.g., StringProperty, ConversationProperty).
  • Established public exports for types and schema definitions.
autoblocks/_impl/datasets_v2/types.py
autoblocks/_impl/datasets_v2/schema.py
autoblocks/datasets_v2/types.py
autoblocks/datasets_v2/schema.py
autoblocks/datasets_v2/__init__.py
Added validation logic for conversation data.
  • Implemented validate_conversation function.
  • Checks conversation structure, roles, turn sequence, and message content.
  • Returns a validation result dictionary.
autoblocks/_impl/datasets_v2/validation.py
autoblocks/datasets_v2/__init__.py
Introduced unit tests for the new client and validation.
  • Added tests for client initialization and core methods (list, create, create_items) using pytest and unittest.mock.
  • Added tests for validate_conversation with valid and invalid inputs.
tests/datasets_v2/test_client.py
tests/datasets_v2/test_validation.py
Provided an example script demonstrating client usage.
  • Shows how to initialize the client.
  • Demonstrates creating datasets, adding items (including conversations), listing datasets, and retrieving items.
examples/datasets_v2_example.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@Hellebore Hellebore left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @Hellebore - I've reviewed your changes - here's some feedback:

  • Consider raising more specific exception types from _make_request instead of a generic Exception to allow for better error handling by the caller.
  • Ensure all dynamic parts of API request paths, including app_slug, are consistently URL-encoded.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟡 Testing: 1 issue found
  • 🟢 Documentation: all looks good

LangSmith trace

Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +79 to +80
if not response.ok:
raise Exception(f"HTTP Request Error: {method} {url} \"{response.status_code} {response.reason}\"")

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Raise a more specific exception instead of a generic Exception.

Define and use a custom or specific exception so SDK clients can catch API errors precisely.

Suggested implementation:

import requests

class APIRequestError(Exception):
    """Exception raised when an API request fails."""
    def __init__(self, method, url, status_code, reason):
        message = f"HTTP Request Error: {method} {url} \"{status_code} {reason}\""
        super().__init__(message)
        self.method = method
        self.url = url
        self.status_code = status_code
        self.reason = reason
        if not response.ok:
            raise APIRequestError(method, url, response.status_code, response.reason)

Make sure to update any exception handling logic in the SDK clients to catch APIRequestError instead of a generic Exception if needed.

Comment on lines +27 to +36
@mock.patch('requests.request')
def test_list_datasets(mock_request):
"""Test listing datasets"""
# Setup mock response
mock_response = mock.Mock()
mock_response.ok = True
mock_response.json.return_value = [
{
"id": "dataset-id-1",
"external_id": "test-dataset-1",

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (testing): Missing test for API error response

Add a test for the error path by mocking mock_response.ok = False and mock_response.status_code = 500, then assert the expected exception is raised.

Suggested implementation:

@mock.patch('requests.request')
def test_list_datasets_error(mock_request):
    """Test listing datasets when API returns an error response"""
    import pytest
    # Setup mock response to simulate error case
    mock_response = mock.Mock()
    mock_response.ok = False
    mock_response.status_code = 500
    mock_response.text = "Internal Server Error"
    mock_request.return_value = mock_response

    client = DatasetsV2Client({
        "api_key": "test-api-key",
        "app_slug": "test-app"
    })

    # Replace Exception with the expected exception type as per your implementation
    with pytest.raises(Exception):
        client.list_datasets()

Ensure that your DatasetsV2Client.list_datasets() method raises an exception when the response is not OK. Replace Exception with the specific exception if one is already defined.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants